package com.resume.controller;

import com.resume.bean.Result;
import com.resume.bean.ResultUtil;
import com.resume.constant.GlobalConst;
import com.resume.domain.City;
import com.resume.domain.CityQuery;
import com.resume.domain.Country;
import com.resume.domain.CountryQuery;
import com.resume.domain.Province;
import com.resume.domain.ProvinceQuery;
import com.resume.domain.ProvinceUpdate;
import com.resume.enums.ExceptionEnum;
import com.resume.enums.ResultEnum;
import com.resume.ex.ResumeException;
import com.resume.service.CityService;
import com.resume.service.CountryService;
import com.resume.service.ProvinceService;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 梁云亮
 * @Date: 2021/7/19 17:04
 * @Describe:
 */
@Controller
@RequestMapping("/province")
public class ProvinceController {

    @Resource
    private ProvinceService provinceService;
    @Resource
    private CityService cityService;
    @Resource
    private CountryService countryService;

    @ApiOperation(value = "打开修改省份的页面", notes = "打开修改省份的页面")
    @GetMapping("/v1/openProvinceCityCountry")
    public String openProvinceCityCountry(Integer provinceId, Integer cityId, Model model) {
        //获取省份
        ProvinceQuery provinceQuery = ProvinceQuery.builder().build();
        List<Province> provinceList = provinceService.listProvince(provinceQuery);

        model.addAttribute("provinceList", provinceList);

        if (provinceId == null) {
            provinceId = provinceList.get(0).getId();
        }
        model.addAttribute("provinceSelected",provinceId);
        //获取城市
        CityQuery cityQuery = CityQuery.builder()
                .provinceId(provinceId)
                .build();
        List<City> cityList = cityService.listCity(cityQuery);

        model.addAttribute("cityList", cityList);

        if (cityId == null) {
            cityId = cityList.get(0).getId();
        }
        model.addAttribute("citySelected",cityId);
        //获取县区
        CountryQuery countryQuery = CountryQuery.builder()
                .cityId(cityId)
                .build();
        List<Country> countryList = countryService.listCountry(countryQuery);
        model.addAttribute("countryList", countryList);

        return "province_city_country";
    }

    @ApiOperation(value = "打开修改省份的页面", notes = "打开修改省份的页面")
    @GetMapping("/v1/openProvinceUpdate")
    public String openProvinceUpdate(Integer id, Model model) {
        Province province = provinceService.getById(id);
        model.addAttribute("province", province);
        return "province_update";
    }

    @ApiOperation(value = "打开添加省份的页面", notes = "打开添加省份的页面")
    @GetMapping("/v1/openProvinceAdd")
    public String openProvinceAdd() {
        return "province_add";
    }

    //列出所有的省
    //列出平台开通的所有的省
    //列出未开通的省
    @ApiOperation(value = "根据条件查询省份信息", notes = "根据条件查询省份信息")
    @GetMapping("/v1/listProvince")
    public String listProvince(ProvinceQuery provinceQuery, Model model) {
//        List<Province> provinceList = provinceService.listProvince(provinceQuery);
//        model.addAttribute("provinceList", provinceList);
        return "province";
    }

    //修改省信息
    @ApiOperation(value = "修改省份", notes = "修改省份")
    @ResponseBody
    @PostMapping("/v1/modifyProvince")
    public Result<String> modifyProvince(ProvinceUpdate provinceUpdate) {
        try {
            provinceService.modifyProvince(provinceUpdate);
            Result<String> result = ResultUtil.success(ResultEnum.UPDATE_SUCCESS);
            return result;
        } catch (Exception e) {
            throw new ResumeException(ExceptionEnum.SQL_ERROR);
        }
    }

    //开疆拓土：新建省
    @ApiOperation(value = "添加省份", notes = "添加省份")
    @ResponseBody
    @PostMapping("/v1/addProvince")
    public Result addProvince(Province province) {
        try {
            province.setStatus(GlobalConst.Common.STATUS_ENABLE);
            provinceService.save(province);
            Result result = ResultUtil.success(ResultEnum.UPDATE_SUCCESS);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            throw new ResumeException(ExceptionEnum.SQL_ERROR);
        }
    }

}